﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Management;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace cry
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
           
        }
        

        private string Encrypt(string clearText)
        {
            string EncryptionKey = "LCFC1314521";
            byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
            using (Aes encryptor = Aes.Create())
            {
                Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
                encryptor.Key = pdb.GetBytes(32);
                encryptor.IV = pdb.GetBytes(16);
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(clearBytes, 0, clearBytes.Length);
                        cs.Close();
                    }
                    clearText = Convert.ToBase64String(ms.ToArray());
                }
            }
            return clearText;
        }

        private string Decrypt(string cipherText)
        {
            string EncryptionKey = "LCFC1314521";
            cipherText = cipherText.Replace(" ", "+");
            try
            {
                Convert.FromBase64String(cipherText);
            }
            catch (Exception )
            {
                MessageBox.Show("The license code is not available!!!");
                return "";
            }
            byte[] cipherBytes = Convert.FromBase64String(cipherText);
            using (Aes encryptor = Aes.Create())
            {
                Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
                encryptor.Key = pdb.GetBytes(32);
                encryptor.IV = pdb.GetBytes(16);
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(cipherBytes, 0, cipherBytes.Length);
                        cs.Close();
                    }
                    cipherText = Encoding.Unicode.GetString(ms.ToArray());
                }
            }
            return cipherText;
        }



        private void button1_Click(object sender, EventArgs e)
        {
            string xxx= Util.GetSystemInfo();
            textBox1.Text = xxx+ "\n\n";
            string ToolTip1 = Encrypt(xxx);
            textBox2.Text = ToolTip1;
            string ToolTip2 = Decrypt(ToolTip1);
            textBox1.Text += ToolTip1 + "\n\n";
            textBox1.Text += ToolTip2 + "\n\n";
          //  MessageBox.Show(xxx);
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox3.Text)) {
                return;
            }
            if (string.IsNullOrEmpty(textBoxmk.Text))
            {
                return;
            }
            else if(textBoxmk.Text != "1314")
            {
                return;
            }
            
           
            string xxx = textBox3.Text.Trim(); ;
                    textBox1.Text = xxx+ "\n\n";
                    string ToolTip1 = Encrypt(xxx);
                    textBox2.Text = ToolTip1;
                    string ToolTip2 = Decrypt(ToolTip1);
                    textBox1.Text += ToolTip1 + "\n\n";
                    textBox1.Text += ToolTip2 + "\n\n";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            CryWork.Init();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            CryWork.Test();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            CryWork.Test1();
        }
    }
}
